package com.example.zuoye;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;

public class MineActivity extends AppCompatActivity {

    public int Sign_RequestCode=1;
    public int CHOSE_PHOTO=100;
    private ImageButton loadpicture;
    private Button shenfen,record,exit,myclass,aboutus;
    private TextView xinxi,loadpic;
    private Uri ImageUri;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);

        xinxi=(TextView)findViewById(R.id.xinxi);
        loadpic=(TextView)findViewById(R.id.loadpic);
        loadpicture = (ImageButton) findViewById(R.id.loadpicture);
       // loadpicture.setOnClickListener(piconClickListen);
        //初始化"读取联系人"组件并注册事件监听器
        shenfen = (Button)findViewById(R.id.shenfen);
        shenfen.setOnClickListener(onClickListening);
        //初始化"管理联系人"组件并注册事件监听器
        record = (Button)findViewById(R.id.record);
        record.setOnClickListener(onClickListening);
        //初始化"管理多媒体"组件并注册事件监听器
        exit = (Button)findViewById(R.id.exit);
        exit.setOnClickListener(onClickListening);

        myclass = (Button)findViewById(R.id.myclass);
        myclass.setOnClickListener(onClickListening);

        aboutus=(Button)findViewById(R.id.aboutus);
        aboutus.setOnClickListener(onClickListening);

        loadpicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choose_photo();
            }
        });

    }

    private void choose_photo() //启动挑选图片功能
    {
        if (ContextCompat.checkSelfPermission(MineActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) //先赋予访问相册的权限
        {
            ActivityCompat.requestPermissions(MineActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
        else
        {
            openAlbum(); //有权限就执行打开相册的功能
        }
    }
    private void openAlbum() //打开手机本地相册
    {
        Intent intent=new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent,CHOSE_PHOTO);
    }


    private View.OnClickListener onClickListening=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            switch (v.getId()) {
                case R.id.shenfen:
                    intent.setClass(MineActivity.this, RegistActivity.class);//身份处理界面
                    startActivityForResult(intent,Sign_RequestCode);
                    break;
                case R.id.record:
                    intent.setClass(MineActivity.this, RecordActivity.class); //学习记录，从数据库中找到我的学习记录
                    startActivity(intent);
                    break;
                case R.id.myclass:
                    intent.setClass(MineActivity.this, MyclassActivity.class);//管理我的课程，以ListView的形式实现
                    startActivity(intent);
                    break;
                case R.id.aboutus:
                    intent.setClass(MineActivity.this,AboutActivity.class); //做这个项目的感想
                    startActivity(intent);
                    break;
                case R.id.exit:
                    intent.setClass(MineActivity.this,RegistActivity.class); //登录界面
                    xinxi.setText("您还未登录，请先登录！");
                    Toast.makeText(MineActivity.this,"退出成功！",Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==Sign_RequestCode)
        {
            if(resultCode==RESULT_OK)
            {
                String name=data.getStringExtra("name");
               xinxi.setText(name+"先生，"+"欢迎您使用该答题小程序！");
                loadpic.setText("请上传您喜欢的图片");
            }
        }
        else if(requestCode==CHOSE_PHOTO)
        {
            try
            {
                Uri uri=data.getData();
                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                Toast.makeText(MineActivity.this,"图片上传成功！",Toast.LENGTH_SHORT).show();
                //setImgSize(bitmap,10);
                loadpic.setText("");
                loadpicture.setImageBitmap(bitmap);
            }catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
        }
    }

  /*  public Bitmap setImgSize(Bitmap bm,int scale) //图片等比例缩放
    {
        // 获得图片的宽高.
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例.
        float k = ((float) scale) / width;
        // 取得想要缩放的matrix参数.
        Matrix matrix = new Matrix();
        matrix.postScale(k, k);
        // 得到新的图片.
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }*/

}

